package com.edushare.common.dao;

import java.util.ArrayList;
import java.util.List;

import com.edushare.common.exceptions.ProcessingException;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollOptions;
import com.edushare.common.vo.PollQuestions;
import com.edushare.common.vo.ResponseSummary;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class PollStatsDAO extends CayenneBasedDAO {

	public PollStatsDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	public int getTotalPolls(Departments dept){
		return new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_DEPT, dept.getDepartmentId()).size();
	}
	
	public int getTotalPolls(Universities univ){
		return new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_UNIV, univ.getUniversityId()).size();
	}
	
	public int getTotalPolls(CourseInstances course){
		return new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_COURSE, course.getCourseInstanceId()).size();
	}
	
	public int getDraftPolls(Departments dept){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_DEPT, dept.getDepartmentId());
		return countState(pollList, Poll.POLL_STATUS_DRAFT);
	}
	
	public int getDraftPolls(Universities univ){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_UNIV, univ.getUniversityId());
		return countState(pollList, Poll.POLL_STATUS_DRAFT);		
	}
	
	public int getDraftPolls(CourseInstances course){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_COURSE, course.getCourseInstanceId());
		return countState(pollList, Poll.POLL_STATUS_DRAFT);		
	}
	
	public int getOpenPolls(Departments dept){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_DEPT, dept.getDepartmentId());
		return countState(pollList, Poll.POLL_STATUS_POLLING);	
	}
	
	public int getOpenPolls(Universities univ){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_UNIV, univ.getUniversityId());
		return countState(pollList, Poll.POLL_STATUS_POLLING);	
	}
	
	public int getOpenPolls(CourseInstances course){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
			(Poll.POLL_COURSE, course.getCourseInstanceId());
		return countState(pollList, Poll.POLL_STATUS_POLLING);					
	}
	
	public int getClosedPolls(Departments dept){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
		(Poll.POLL_DEPT, dept.getDepartmentId());
		return countState(pollList, Poll.POLL_STATUS_CLOSED);		
	}
	
	public int getClosedPolls(Universities univ){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
		(Poll.POLL_UNIV, univ.getUniversityId());
		return countState(pollList, Poll.POLL_STATUS_CLOSED);
	}
	
	public int getClosedPolls(CourseInstances course){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator
		(Poll.POLL_COURSE, course.getCourseInstanceId());
		return countState(pollList, Poll.POLL_STATUS_CLOSED);			
	}
	
	public int getTotalUserCreatedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator(user);
		return pollList.size();
	}
	
	public int getDraftUserCreatedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator(user);
		return countState(pollList, Poll.POLL_STATUS_DRAFT);
	}
	
	public int getOpenUserCreatedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator(user);
		return countState(pollList, Poll.POLL_STATUS_POLLING);		
	}

	public int getClosedUserCreatedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByCreator(user);
		return countState(pollList, Poll.POLL_STATUS_CLOSED);				
	}
	
	public int getTotalUserRespondedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByUser(user);
		return pollList.size();
	}
	
	public int getOpenUserRespondedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByUser(user);
		return countState(pollList, Poll.POLL_STATUS_POLLING);
	}
	
	public int getClosedUserRespondedPolls(Users user){
		List<Poll> pollList = new PollsDAO(ormInterface).getPollsByUser(user);
		return countState(pollList, Poll.POLL_STATUS_CLOSED);
	}
	
	private int countState(List<Poll> pollList, int state){
		int i=0;
		for(Poll p:pollList){
			if(p.getStatus() == state){
				i++;
			}
		}
		return i;		
	}

	public int getTotalPolls(Groups group){
		return 
			new 
				PollsDAO(ormInterface)
				.getPollsByCreator(Messages.RECEIVER_TYPE_GROUP, group.getGroupId())
				.size();
	}
		
	public int getClosedPolls(Groups group) {
		List<Poll> pollList = new PollsDAO(ormInterface)
							.getPollsByCreator(Messages.RECEIVER_TYPE_GROUP, group.getGroupId());
		return countState(pollList, Poll.POLL_STATUS_CLOSED);
	}
		
	public int getOpenPolls(Groups group) {
		List<Poll> pollList = new PollsDAO(ormInterface)
							.getPollsByCreator(Messages.RECEIVER_TYPE_GROUP, group.getGroupId());
		return countState(pollList, Poll.POLL_STATUS_POLLING);
	}
		
	public int getDraftPolls(Groups group) {
		List<Poll> pollList = new PollsDAO(ormInterface)
							.getPollsByCreator(Messages.RECEIVER_TYPE_GROUP, group.getGroupId());
		return countState(pollList, Poll.POLL_STATUS_DRAFT);
	}
	
	public List<ResponseSummary> getSummarizedResponse(PollQuestions q) throws ProcessingException{
		if(!q.isResponseSummarizable())
			throw new ProcessingException();
		
		List<ResponseSummary> sumList = new ArrayList<ResponseSummary>();
		
		if(		q.getQuestionType() == PollQuestions.QUESTION_TYPE_DROPDOWN || 
				q.getQuestionType() == PollQuestions.QUESTION_TYPE_MULTIPLE_CHOICE || 
				q.getQuestionType() == PollQuestions.QUESTION_TYPE_SINGLE_CHOICE){
			
			for(PollOptions po: q.getOptions()){
				ResponseSummary sum = new ResponseSummary();
				sum.setResponseText(po.getOptionString());
				UserPollDAO dao = new UserPollDAO(ormInterface);
				int votes = dao.getResponsesByOption(po);
				sum.setVotes(votes);
				int maxVotes = dao.getMaxOptions(q);
				sum.setPercent(100.0*votes/maxVotes);
				sumList.add(sum);
			}
			
		}else if(q.getQuestionType() == PollQuestions.QUESTION_TYPE_RATING){
			ResponseSummary sum = new ResponseSummary();
			sum.setResponseText("Average Rating ");
			UserPollDAO dao = new UserPollDAO(ormInterface);
			int votes = dao.getMaxOptions(q);
			sum.setVotes(votes);
			double avgRating = dao.getAverageRating(q);
			sum.setRating(avgRating);
			sumList.add(sum);
		}
		
		return sumList;
	}
	
}
